package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 惠阳之窗
 * @author Persh
 *
 */
public class SohuiyangNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.sohuiyang.com/.*newsShow.asp\\?dataID=[0-9]*"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.kdtech.analyse.AnalyseInterface#parserHtml(com.kdtech.entity.crawler.UrlMeta)
	 */
	
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#newsContent"));
		date=DateUtils.matchDate(doc.select("div.addi2").text());

		if(StringUtils.isBlank(title)){
		}
		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 * 
		 * */
		String str=null;
		String author=null;
		str=doc.select(".addi2").text();
		author=StringUtils.substringBetween(str,"来源：","查看");
		news.setAuthor(author);
		/**
		 *                        	 **************************end *************************
		 * 
		 **/


		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		SohuiyangNewsAnalyse analyse=new SohuiyangNewsAnalyse();
		String url="http://www.sohuiyang.com/sh/newsShow.asp?dataID=3585";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
